package xna.android;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;

public class Sprite {
	
	public enum ShotType{ RED, BLUE,}
	
	protected ShotType type;
	protected Bitmap bitmap;		// imagem
	protected int posX;				// coord X
	protected int posY;				// Y
	protected boolean touched;	// para o toque
	protected Speed speed;
	protected boolean visible;
	
	protected SoundManager soundManager;
	
	public Sprite(Bitmap bitmap, int x, int y, SoundManager sm, Speed speed)
	{
		this.bitmap = bitmap;
		this.posX = x;
		this.posY = y;
		this.speed = speed;
		this.visible = true;
		this.soundManager = sm;
	}

	public Bitmap getBitmap()
	{
		return bitmap;
	}
	
	public void setBitmap(Resources res, int texture)
	{
		this.bitmap = BitmapFactory.decodeResource(res, texture);
	}
	
	public ShotType getType() {
		return type;
	}
	public void setType(ShotType type) {
		this.type = type;
	}
	public int getX()
	{
		return posX;
	}
	
	public void setX(int x)
	{
		this.posX = x;
	}
	
	public int getY()
	{
		return posY;
	}
	
	public void setY(int y)
	{
		this.posY = y;
	}
	
	public boolean isTouched()
	{
		return touched;
	}

	public void setTouched(boolean touched)
	{
		this.touched = touched;
	}

	public boolean isVisible() {
		return visible;
	}

	public void setVisible(boolean visible) {
		this.visible = visible;
	}

	public Speed getSpeed() {
		return speed;
	}

	public void setSpeed(Speed speed) {
		this.speed = speed;
	}

	public SoundManager getSoundManager() {
		return soundManager;
	}

	public void setSoundManager(SoundManager soundManager) {
		this.soundManager = soundManager;
	}

	public void handleActionDown(int eventX, int eventY)
	{
		if ((eventX >= (posX - bitmap.getWidth() / 2))
		&& (eventX <= (posX + bitmap.getWidth() / 2))
		&& (eventY >= (posY - bitmap.getHeight() / 2))
		&& (eventY <= (posY + bitmap.getHeight() / 2)))
		{
			setTouched(true);
		} 
		else 
			setTouched(false);
	}

	public void draw(Canvas canvas)
	{
		if(visible)
		canvas.drawBitmap(bitmap, posX - (bitmap.getWidth() / 2), posY - (bitmap.getHeight() / 2), null);
	}
		
	public boolean intersects(Sprite gameComponent)
	{
		if((this.posX+this.bitmap.getWidth()<gameComponent.posX)//fora pela esquerda
		|| (this.posX> gameComponent.posX + gameComponent.bitmap.getWidth())//fora pela direita
		|| (this.posY+this.bitmap.getHeight()< gameComponent.posY)//fora por cima
		|| (this.posY> gameComponent.posY + gameComponent.bitmap.getHeight()))//fora por baixo
		{
			return false;
		}
		else
			return true;
	}
	
	protected void collideOnWall(int windowWidth, int windowHeight){
		
		if(posX<0 || posX>windowWidth)
			speed.toggleXDirection();
				
		if(posY<0 || posY>windowHeight)
			speed.toggleYDirection();
	}
	
	public void update()
	{
		if (speed != null){
			posX+=speed.getXv()*speed.getxDirection();
			posY+=speed.getYv()*speed.getyDirection();
		}
	}		
}